/* 
 * Copyright 2009 Marc Englund <marc.englundATitmill.com>
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.vaadin.simplegesture;

import org.vaadin.simplegesture.SimpleGesture.GestureEvent;

import com.vaadin.Application;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Window;

public class SimpleGestureDemo extends Application {

    // The actual gesture recognizer
    private SimpleGesture sg = new SimpleGesture();

    private Button record = new Button("Record gesture",
            new Button.ClickListener() {
                public void buttonClick(ClickEvent event) {
                    // toggles between record/cancel
                    if (sg.isRecordMode()) {
                        sg.setRecordMode(false);
                        event.getButton().setCaption("Record gesture");
                    } else {
                        sg.setRecordMode(true);
                        event.getButton().setCaption("Cancel recording");
                    }
                }
            });

    @Override
    public void init() {
        Window mainWindow = new Window("SimpleGesture Demo");
        setMainWindow(mainWindow);

        mainWindow.getContent().setWidth("500px");
        mainWindow.getContent().setHeight("500px");

        // Set up some initial gestures
        sg.addGesture("5443", "Counter clockwise"); // nw, w, w, sw
        sg.addGesture("7001", "Clockwise"); // ne, e, e, se
        sg.addGesture("321076543", "Circle"); // etc
        sg.addGesture("43210134", "Squiggle");

        // The listener that reacts to gestures - we'll just show the gesture
        // name as a notification.
        sg.addListener(new SimpleGesture.GestureListener() {
            public void gesture(GestureEvent event) {
                if (sg.isRecordMode()) {
                    sg.setRecordMode(false);
                    record.setCaption("Record gesture");
                    getMainWindow().addComponent(
                            new AddGesture(event.getMoved()));
                } else {
                    getMainWindow()
                            .showNotification(event.getData().toString());
                }
            }
        });
        // The component is invisible but needs to be in the layout anyway.
        mainWindow.addComponent(sg);

        mainWindow.addComponent(record);
    }

    // Helper component for adding recorded gestures w/ name
    private class AddGesture extends HorizontalLayout {
        public AddGesture(final String gesture) {
            addComponent(new Label(gesture));

            final TextField name = new TextField();
            name.setInputPrompt("Gesture Name");
            addComponent(name);

            final Button cancel = new Button("Cancel",
                    new Button.ClickListener() {
                        public void buttonClick(ClickEvent event) {
                            getMainWindow().removeComponent(AddGesture.this);
                        }
                    });
            addComponent(cancel);

            final Button save = new Button("Save", new Button.ClickListener() {
                public void buttonClick(ClickEvent event) {
                    sg.addGesture(gesture, name.getValue());
                    getMainWindow().removeComponent(AddGesture.this);
                }
            });
            addComponent(save);

        }
    }

}
